﻿using AR.Entities;
using AR.Model;
using AR.ViewModel.Commands.Candidat;
using AR.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AR.Validation;
using AR.Models;
using System.IO;
using System.Text;
using System.Collections.Generic;
using AR.Web.Services;
using System.Linq;
using GalaSoft.MvvmLight.Command;
using System.ComponentModel.DataAnnotations;

namespace AR.ViewModel.Candidat
{
    public class CreationCandidatViewModel : ViewModelBase
    {

        #region Declarations

        // Test Check out 

        private ObservableCollection<TAG> _TagsTrouves;
        public ObservableCollection<TAG> TagsTrouves
        {
            get { return _TagsTrouves; }
            set
            {
                if (_TagsTrouves != value)
                {
                    _TagsTrouves = value;
                    OnPropertyChanged("TagsTrouves");
                }
            }
        }
        private ObservableCollection<TAG> _SelectedTags;
        public ObservableCollection<TAG> SelectedTags
        {
            get { return _SelectedTags; }
            set
            {
                if (_SelectedTags != value)
                {
                    _SelectedTags = value;
                    OnPropertyChanged("SelectedTags");
                }
            }
        }
        private bool _cvPathReadOnly;
        public bool CvPathReadOnly
        {

            get { return _cvPathReadOnly; }
            set
            {
                _cvPathReadOnly = value;
                OnPropertyChanged("CvPathReadOnly");
            }
        }
        private string _textButton;
        public string TextButton
        {

            get { return _textButton; }
            set
            {
                _textButton = value;
                OnPropertyChanged("TextButton");
            }
        }
        private static int _idCandidat;
        public static int IdCandidat
        {

            get { return _idCandidat; }
            set
            {
                _idCandidat = value;
                ///  OnPropertyChanged("IdCandidat");
            }
        }

        //public  CANDIDAT candidatparam { get; set; }
        private CANDIDAT _candidatparam;
        public CANDIDAT candidatparam
        {

            get { return _candidatparam; }
            set
            {
                _candidatparam = value;
                OnPropertyChanged("candidatparam");
            }
        }
        //public static int nbinstance = 0;
        public static List<String> usedtestcode = new List<string>();
        private CandidatModel model;
        private FileInfo _file;

        private string fileExtension;
        private PRACTICE _practice;
        public PRACTICE Practice
        {
            get
            {
                return _practice;
            }
            set
            {
                if (_practice != value)
                {
                    _practice = value;
                    OnPropertyChanged("Practice");
                    RemplirTestPractice();
                }
            }
        }

        private string _poste;
        public string Poste
        {
            get
            {
                return _poste;
            }
            set
            {
                _poste = value;
                OnPropertyChanged("Poste");

            }
        }
        private string _projet;
        public string Projet
        {
            get
            {
                return _projet;
            }
            set
            {
                _projet = value;
                OnPropertyChanged("Projet");

            }

        }
        private string _cvpath;
        public string CvPath
        {
            get
            {
                return _cvpath;
            }
            set
            {
                _cvpath = value;
                OnPropertyChanged("CvPath");

            }
        }
        private TEST_TECHNIQUE _test;
        public TEST_TECHNIQUE Test
        {
            get { return _test; }
            set
            {
                _test = value;
                OnPropertyChanged("Test");
            }
        }
        private ObservableCollection<TEST_TECHNIQUE> _tests;
        public ObservableCollection<TEST_TECHNIQUE> Tests
        {
            get { return _tests; }
            set
            {
                _tests = value;
                OnPropertyChanged("Tests");
            }
        }
        private ObservableCollection<PRACTICE> _practices;
        public ObservableCollection<PRACTICE> Practices
        {
            get { return _practices; }
            set
            {
                _practices = value;
                OnPropertyChanged("Practices");
            }
        }
        private ObservableCollection<decimal> _gcmCollec;
        public ObservableCollection<decimal> GcmCollec
        {
            get { return _gcmCollec; }
            set
            {
                _gcmCollec = value;
                OnPropertyChanged("GcmCollec");
            }
        }
        private ObservableCollection<POSTE> _postes;
        public ObservableCollection<POSTE> Postes
        {
            get { return _postes; }
            set
            {
                _postes = value;
                OnPropertyChanged("Postes");
            }
        }
        private ObservableCollection<PROJET> _projets;
        public ObservableCollection<PROJET> Projets
        {
            get { return _projets; }
            set
            {
                _projets = value;
                OnPropertyChanged("Projets");
            }
        }

        public ObservableCollection<NIVEAU_EXPERIENCE> _niveauxExperience;
        public ObservableCollection<NIVEAU_EXPERIENCE> NiveauxExperience
        {

            get { return _niveauxExperience; }
            set
            {
                _niveauxExperience = value;
                OnPropertyChanged("NiveauxExperience");
            }
        }
        public ObservableCollection<NIVEAU_ETUDE> _niveauxEtudes;
        public ObservableCollection<NIVEAU_ETUDE> NiveauxEtudes
        {


            get { return _niveauxEtudes; }
            set
            {
                _niveauxEtudes = value;
                OnPropertyChanged("NiveauxEtudes");
            }
        }

        private CANDIDAT _candidat;
        public CANDIDAT Candidat
        {
            get
            {
                return _candidat;

            }
            set
            {
                _candidat = value;
                OnPropertyChanged("Candidat");

            }
        }
        private CANDIDATClient _candidatClient;
        public CANDIDATClient CandidatClient
        {
            get
            {
                return _candidatClient;

            }
            set
            {
                _candidatClient = value;
                OnPropertyChanged("CandidatClient");

            }

        }
        private string _firstNiveau;
        public string FirstNiveau
        {
            get
            {
                return _firstNiveau;
            }
            set
            {
                _firstNiveau = value;
                OnPropertyChanged("FirstNiveau");
            }
        }
        private TEST_TECHNIQUE _testTechnique;
        public TEST_TECHNIQUE TestTechnique
        {
            get
            {
                return _testTechnique;
            }
            set
            {
                if (_testTechnique != value)
                {
                    _testTechnique = value;
                    OnPropertyChanged("TestTechnique");
                }
            }
        }

        private string _searchTagsTxt;
        public string SearchTagsTxt
        {
            get { return _searchTagsTxt; }
            set
            {
                if (_searchTagsTxt != value)
                {
                    _searchTagsTxt = value;
                    OnPropertyChanged("SearchTagsTxt");
                }
            }
        }
        private ObservableCollection<TAG> _Tags;
        public ObservableCollection<TAG> Tags
        {
            get { return _Tags; }
            set
            {
                if (_Tags != value)
                {
                    _Tags = value;
                    OnPropertyChanged("Tags");
                }
            }
        }
        private ObservableCollection<string> _TagsName;
        public ObservableCollection<string> TagsName
        {
            get { return _TagsName; }
            set
            {
                if (_TagsName != value)
                {
                    _TagsName = value;
                    OnPropertyChanged("TagsName");
                }
            }
        }
        private TAG _TagCandidat;
        public TAG TagCandidat
        {
            get { return _TagCandidat; }
            set
            {
                if (_TagCandidat != value)
                {
                    _TagCandidat = value;
                    OnPropertyChanged("TagCandidat");
                }
            }
        }


        private ObservableCollection<TEST_TECHNIQUE> _TestsPractice;
        public ObservableCollection<TEST_TECHNIQUE> TestsPractice
        {
            get { return _TestsPractice; }
            set
            {
                if (_TestsPractice != value)
                {
                    _TestsPractice = value;
                    OnPropertyChanged("TestsPractice");
                }
            }
        }
        #region Visibility Properties
        private Visibility _updateVisibility;
        public Visibility UpdateVisibility
        {
            get
            {
                return _updateVisibility;

            }
            set
            {
                _updateVisibility = value;
                OnPropertyChanged("UpdateVisibility");

            }
        }

        private Visibility _errNomVis;
        public Visibility ErrNomVis
        {
            get
            {
                return _errNomVis;

            }
            set
            {
                _errNomVis = value;
                OnPropertyChanged("ErrNomVis");

            }
        }
        private Visibility _errFichCvVis;
        public Visibility ErrFichCvVis
        {
            get
            {
                return _errFichCvVis;

            }
            set
            {
                _errFichCvVis = value;
                OnPropertyChanged("ErrFichCvVis");

            }
        }
        private Visibility _errCvVis;
        public Visibility ErrCvVis
        {
            get
            {
                return _errCvVis;

            }
            set
            {
                _errCvVis = value;
                OnPropertyChanged("ErrCvVis");

            }
        }
        private Visibility _errPrenomVis;
        public Visibility ErrPrenomVis
        {
            get
            {
                return _errPrenomVis;

            }
            set
            {
                _errPrenomVis = value;
                OnPropertyChanged("ErrPrenomVis");

            }
        }
        private Visibility _errDateNaissance;
        public Visibility ErrDateNaissanceVis
        {
            get
            {
                return _errDateNaissance;

            }
            set
            {
                _errDateNaissance = value;
                OnPropertyChanged("ErrDateNaissance");

            }
        }
        private Visibility _errDernierEmpVis;
        public Visibility ErrDernierEmpVis
        {
            get
            {
                return _errDernierEmpVis;

            }
            set
            {
                _errDernierEmpVis = value;
                OnPropertyChanged("ErrDernierEmpVis");

            }
        }
        private Visibility _errSalaireActVis;
        public Visibility ErrSalaireActVis
        {
            get
            {
                return _errSalaireActVis;

            }
            set
            {
                _errSalaireActVis = value;
                OnPropertyChanged("ErrSalaireActVis");

            }
        }
        private Visibility _errSalaireDemVis;
        public Visibility ErrSalaireDemVis
        {
            get
            {
                return _errSalaireDemVis;

            }
            set
            {
                _errSalaireDemVis = value;
                OnPropertyChanged("ErrSalaireDemVis");

            }
        }
        private Visibility _errPreavisVis;
        public Visibility ErrPreavisVis
        {
            get
            {
                return _errPreavisVis;
            }
            set
            {
                _errPreavisVis = value;
                OnPropertyChanged("ErrPreavisVis");

            }
        }
        private Visibility _errTelVis;
        public Visibility ErrTelVis
        {
            get
            {
                return _errTelVis; ;

            }
            set
            {
                _errTelVis = value;
                OnPropertyChanged("ErrTelVis");

            }
        }
        private Visibility _errEmailVis;
        public Visibility ErrEmailVis
        {
            get
            {
                return _errEmailVis;

            }
            set
            {
                _errEmailVis = value;
                OnPropertyChanged("ErrEmailVis");

            }
        }
        private Visibility _errDateEntTechniqueVis;
        public Visibility ErrDateEntTechniqueVis
        {
            get
            {
                return _errDateEntTechniqueVis;

            }
            set
            {
                _errDateEntTechniqueVis = value;
                OnPropertyChanged("ErrDateEntTechniqueVis");

            }
        }
        private Visibility _errDateEntRHVis;
        public Visibility ErrDateEntRHVis
        {
            get
            {
                return _errDateEntRHVis;

            }
            set
            {
                _errDateEntRHVis = value;
                OnPropertyChanged("ErrDateEntRHVis");

            }
        }





        #endregion
        List<CANDIDAT_TAG> CandidatTagListe = new List<CANDIDAT_TAG>();

        #endregion

        #region Commands
        public ICommand SupressionTagCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.SupressionTag();

                });
            }
        }
        public ICommand RechercheTagsCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.RechercheTags();
                });
            }
        }
        public ICommand AddCandidatCommand
        {
            get
            {
                return new AddCandidat(this);
            }
        }
        public ICommand VisualiserCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.Visualisercv();

                });
            }
        }


        private void Visualisercv()
        {

            string relatifpath = System.Windows.Application.Current.Host.Source.AbsoluteUri.Replace("/ClientBin/AR.xap", "/AppData/CV/");
            //  System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(@"http://localhost:62951/AppData/CV/" + candidatparam.CV), "_blank");
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(relatifpath + candidatparam.CV), "_blank");
        }
        #endregion

        #region fonctions
        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (!usedtestcode.Contains(builder.ToString()))
            {

                usedtestcode.Add(builder.ToString().ToLower());
                return builder.ToString().ToLower();

            }
            else
            {
                return this.RandomString(size);
            }
        }

        public void AddCandidat()
        {
            Candidat.USER_NAME = WebContext.Current.User.Name;
            Candidat.CODE_TEST = this.RandomString(7);
            Candidat.ID_STATUT = 1;

            CandidatContext candidatcontext = new CandidatContext();

            if (CandidatClient.ValidateCandidat(candidatcontext.ValidationContext) == false)
            {
                return;
            }
            else
            {
                Candidat.SALAIRE_DEMANDE = CandidatClient.SALAIRE_DEMANDE;
                Candidat.SALAIRE_ACTUELLE = CandidatClient.SALAIRE_ACTUELLE;
                Candidat.NOM = CandidatClient.NOM;
                Candidat.PRENOM = CandidatClient.PRENOM;
                Candidat.EMAIL = CandidatClient.EMAIL;
                Candidat.TEL = CandidatClient.TEL;
                Candidat.PREAVIS = CandidatClient.PREAVIS;
                Candidat.DERNIER_EMPLOYEUR = CandidatClient.DERNIER_EMPLOYEUR;
                Candidat.ID_POSTE = CandidatClient.IdPoste;
                Candidat.ID_PROJET = CandidatClient.IdProjet;
                Candidat.ID_PRACTICE = CandidatClient.IdPractice;
                Candidat.ID_TEST = CandidatClient.Id_Test;
                Candidat.GCM_ESTIMEE_RH = CandidatClient.GCM_ESTIMEE_RH;

                if (candidatparam == null)
                {
                    // Candidat.CODE_TEST = this.RandomString(7);
                    // Candidat.USER_NAME = WebContext.Current.oUser.Name;
                    Candidat.DATE_CREATION = DateTime.Now;
                    Candidat.CV = Candidat.NOM + "_" + Candidat.PRENOM + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExtension;
                    UploadCommands.UploadFile(Candidat.CV, _file.OpenRead());
                    this.model.AddCandidat(Candidat, SelectedTags.ToList());
                    //model.SubmitChanges();
                }
                else
                {
                    if (fileExtension != null)
                    {
                        Candidat.CV = Candidat.NOM + "_" + Candidat.PRENOM + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExtension;
                        UploadCommands.UploadFile(Candidat.CV, _file.OpenRead());
                    }
                    Candidat.ID_CANDIDAT = candidatparam.ID_CANDIDAT;
                    this.model.UpdateCandidat(Candidat, CandidatClient.CvPath, SelectedTags.ToList());
                    model.SubmitChanges();
                }
            }
        }


        private void RechercheTags()
        {
            TagsTrouves.Clear();
            foreach (TAG item in Tags.Where(t => t.NOM.ToUpper().Contains(SearchTagsTxt.ToUpper())))
            {
                if (!SelectedTags.Contains(item))
                    TagsTrouves.Add(item);
            }
        }

        public ICommand ParcourirClickCommand
        {
            get
            {
                return new ParcourirClick(this);
            }
        }

        public void ParcourirClick()
        {
            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Filter = "Tout les fichiers (*.*)|*.*|Fichiers PDF (*.pdf)|*.pdf|Fichier MS Word (*.docx)|*.doc;*.docx";
            OFD.FilterIndex = 1;
            OFD.Multiselect = false;



            bool? userClickedOk = OFD.ShowDialog();
            if (userClickedOk == true)
            {
                CvPathReadOnly = false;
                CandidatClient.CvPath = OFD.File.Name;
                fileExtension = OFD.File.Extension;
                if (OFD.File.Length < 4194304)
                {
                    _file = OFD.File;
                    ErrFichCvVis = Visibility.Collapsed;
                }
                else
                {
                    ErrFichCvVis = Visibility.Visible;
                    ErrCvVis = Visibility.Collapsed;
                }



            }

        }
        //public CreationCandidatViewModel()
        //{
        //    if (WebContext.Current.User.IsAuthenticated)
        //    {
        //        if (WebContext.Current.User.IsInRole("Admin") || WebContext.Current.User.IsInRole("PracticeManager") || WebContext.Current.User.IsInRole("WorkforceManager"))
        //        {
        //            CvPathReadOnly = false;
        //            InitialisationDonnees(0);
        //        }
        //        else
        //        {
        //            ((MainPage)(Application.Current.RootVisual)).ContentFrame.Navigate(new Uri("/ListeCandidats", UriKind.Relative));
        //        }
        //    }
        //    else
        //    {
        //        ((MainPage)(Application.Current.RootVisual)).ContentFrame.Navigate(new Uri("/LoginPage", UriKind.Relative));
        //    }

        //}

        private async void InitialisationDonnees(int id)
        {
            TagsTrouves = new ObservableCollection<TAG>();
            SelectedTags = new ObservableCollection<TAG>();
            SearchTagsTxt = "";
            model = new CandidatModel();

            Practices = new ObservableCollection<PRACTICE>();
            Postes = new ObservableCollection<POSTE>();
            Projets = new ObservableCollection<PROJET>();
            NiveauxEtudes = new ObservableCollection<NIVEAU_ETUDE>();
            NiveauxExperience = new ObservableCollection<NIVEAU_EXPERIENCE>();
            GcmCollec = new ObservableCollection<decimal> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            TagsName = new ObservableCollection<string>();
            Tags = new ObservableCollection<TAG>();
            TagCandidat = new TAG();
            Tests = new ObservableCollection<TEST_TECHNIQUE>();
            Test = new TEST_TECHNIQUE();
            TestsPractice = new ObservableCollection<TEST_TECHNIQUE>();
            model.GetTestsTechniques().EntityAdded += OnEntityAdded;
            if (id == 0)
            {
                model.GetPostes().EntityAdded += OnEntityAdded;
                model.GetProjets().EntityAdded += OnEntityAdded;
                model.GetNiveauEtudes().EntityAdded += OnEntityAdded;
                model.GetNiveauExperience().EntityAdded += OnEntityAdded;
                model.GetPractices().EntityAdded += OnEntityAdded;
            }
            model.GetStatutsCandidat().EntityAdded += OnEntityAdded;
            model.GetTags().EntityAdded += OnEntityAdded;
            if (id > 0)
            {
                model.GetCandidatTags().EntityAdded += OnEntityAdded;
                model.GetCandidatTags().EntityRemoved += OnEntityRemoved;
            }

            TestTechnique = new TEST_TECHNIQUE();
            Candidat = new CANDIDAT();
            CandidatClient = new CANDIDATClient();
            // _file = new FileInfo("_file");
            Candidat.DATE_ENTRETIEN_RH = DateTime.Today;
            Candidat.DATE_ENTRETIEN_TECHNIQUE = DateTime.Today;
            Candidat.DATENAISSANCE = DateTime.Now.AddYears(-25);
            TextButton = "Ajouter";
            Candidat.GCM_ESTIMEE_RH = GcmCollec[0];
            UpdateVisibility = Visibility.Collapsed;
        }


        public CreationCandidatViewModel(int id)
        {

            if (WebContext.Current.User.IsAuthenticated)
            {
                if (WebContext.Current.User.IsInRole("Admin") || WebContext.Current.User.IsInRole("PracticeManager") || WebContext.Current.User.IsInRole("WorkforceManager"))
                {
                    CvPathReadOnly = true;
                    InitialisationDonnees(id);

                    if (id != 0)
                    {
                        CandidatContext context = new CandidatContext();
                        context.Load<CANDIDAT>(context.GetCandidatByIDQuery(id)).Completed += (sender, arg) =>
                        {
                            candidatparam = context.CANDIDATs.SingleOrDefault();
                            if (candidatparam != null)
                            {
                                SetExitingCandidat();
                                model.GetPractices().EntityAdded += OnEntityAdded;
                                model.GetPostes().EntityAdded += OnEntityAdded;
                                model.GetProjets().EntityAdded += OnEntityAdded;
                                model.GetNiveauEtudes().EntityAdded += OnEntityAdded;
                                model.GetNiveauExperience().EntityAdded += OnEntityAdded;

                                TextButton = "Modifier";
                                UpdateVisibility = Visibility.Visible;

                                foreach (CANDIDAT_TAG item in CandidatTagListe.Where(c => c.ID_CANDIDAT == id))
                                {
                                    TAG t = Tags.Where(e => e.ID_TAG == item.ID_TAG).FirstOrDefault();
                                    if (t != null)
                                        SelectedTags.Add(t);
                                }
                            }
                        };

                        if (candidatparam != null)
                        {
                            TestTechnique = candidatparam.TEST_TECHNIQUE;
                            Candidat.ID_TEST = candidatparam.ID_TEST;
                        }
                    }
                }
                else
                {
                    NavigationService.Navigate("/ListeCandidats");
                }
            }
            else
            {
                NavigationService.Navigate("/LoginPage");
            }
        }

        private void SetExitingCandidat()
        {
            //Candidat = candidatparam;
            CandidatClient = new CANDIDATClient();
            CandidatClient.SALAIRE_DEMANDE = candidatparam.SALAIRE_DEMANDE;
            CandidatClient.SALAIRE_ACTUELLE = candidatparam.SALAIRE_ACTUELLE;
            CandidatClient.NOM = candidatparam.NOM;
            CandidatClient.PRENOM = candidatparam.PRENOM;
            CandidatClient.EMAIL = candidatparam.EMAIL;
            CandidatClient.TEL = candidatparam.TEL;
            CandidatClient.PREAVIS = candidatparam.PREAVIS;
            CandidatClient.DERNIER_EMPLOYEUR = candidatparam.DERNIER_EMPLOYEUR;
            CandidatClient.CvPath = candidatparam.CV;
            CandidatClient.Id_Test = candidatparam.ID_TEST;
            CandidatClient.IdPractice = candidatparam.ID_PRACTICE;
            CandidatClient.IdPoste = candidatparam.ID_POSTE;
            CandidatClient.IdProjet = candidatparam.ID_PROJET;
            CandidatClient.GCM_ESTIMEE_RH = candidatparam.GCM_ESTIMEE_RH;

            //Candidat.NOM = candidatparam.NOM;
            //Candidat.PRENOM = candidatparam.PRENOM;
            //Candidat.EMAIL = candidatparam.EMAIL;
            //Candidat.TEL = candidatparam.TEL;
            //Candidat.DERNIER_EMPLOYEUR = candidatparam.DERNIER_EMPLOYEUR;
            //Candidat.PREAVIS = candidatparam.PREAVIS;
            //Candidat.SALAIRE_ACTUELLE = candidatparam.SALAIRE_ACTUELLE;
            //Candidat.SALAIRE_DEMANDE = candidatparam.SALAIRE_DEMANDE;

            Candidat.DATENAISSANCE = candidatparam.DATENAISSANCE;
            Candidat.GCM_ESTIMEE_RH = candidatparam.GCM_ESTIMEE_RH;
            Candidat.REMARQUE = candidatparam.REMARQUE;
            Candidat.DATE_ENTRETIEN_TECHNIQUE = candidatparam.DATE_ENTRETIEN_TECHNIQUE;
            Candidat.DATE_ENTRETIEN_RH = candidatparam.DATE_ENTRETIEN_RH;
            Candidat.DATE_TEST_TECHNIQUE = candidatparam.DATE_TEST_TECHNIQUE;
            Candidat.DATE_INTEGRATION = candidatparam.DATE_INTEGRATION;
            Candidat.ID_PRACTICE = -1;
            Candidat.ID_ETUDE = -1;
            Candidat.ID_EXPERIENCE = -1;
            Candidat.ID_POSTE = -1;
            Candidat.ID_PROJET = -1;
            Candidat.ID_TEST = -1;
        }

        #endregion

        #region OnEntityAdded(Practice,Poste,Projet)

        private void OnEntityAdded(object sender, EntityCollectionChangedEventArgs<CANDIDAT> e)
        {
            Candidat = e.Entity;
        }
        private void OnEntityAdded(object sender,
                              EntityCollectionChangedEventArgs<PRACTICE> entityCollectionChangedEventArgs)
        {
            Practices.Add(entityCollectionChangedEventArgs.Entity);
            if (candidatparam == null)
            {
                CandidatClient.IdPractice = Practices[0].ID_PRACTICE;
                Practice = Practices[0];
                //Candidat.ID_TEST = Practices.ElementAt(0).TEST_TECHNIQUE.ElementAt(0).ID_TEST;

            }
            else
            {
                if (entityCollectionChangedEventArgs.Entity.ID_PRACTICE == candidatparam.ID_PRACTICE)
                {
                    CandidatClient.IdPractice = entityCollectionChangedEventArgs.Entity.ID_PRACTICE;

                    Practice = candidatparam.PRACTICE;
                    CandidatClient.Id_Test = candidatparam.ID_TEST;
                }
            }
        }
        private void OnEntityAdded(object sender,
                             EntityCollectionChangedEventArgs<POSTE> entityCollectionChangedEventArgs)
        {

            Postes.Add(entityCollectionChangedEventArgs.Entity);
            if (candidatparam == null)
                CandidatClient.IdPoste = Postes[0].IDPOSTE;
            else
            {
                if (entityCollectionChangedEventArgs.Entity.IDPOSTE == candidatparam.ID_POSTE)
                    CandidatClient.IdPoste = entityCollectionChangedEventArgs.Entity.IDPOSTE;
            }

        }
        private void OnEntityAdded(object sender,
                             EntityCollectionChangedEventArgs<PROJET> entityCollectionChangedEventArgs)
        {
            Projets.Add(entityCollectionChangedEventArgs.Entity);
            if (candidatparam == null)
                CandidatClient.IdProjet = Projets[0].IDPROJET;
            else
            {
                if (entityCollectionChangedEventArgs.Entity.IDPROJET == candidatparam.ID_PROJET)
                    CandidatClient.IdProjet = entityCollectionChangedEventArgs.Entity.IDPROJET;
            }
        }
        private void OnEntityAdded(object sender,
                            EntityCollectionChangedEventArgs<NIVEAU_EXPERIENCE> entityCollectionChangedEventArgs)
        {
            NiveauxExperience.Add(entityCollectionChangedEventArgs.Entity);
            if (candidatparam == null)
                Candidat.ID_EXPERIENCE = NiveauxExperience[0].ID_EXPERIENCE;
            else
            {
                if (entityCollectionChangedEventArgs.Entity.ID_EXPERIENCE == candidatparam.ID_EXPERIENCE)
                    Candidat.ID_EXPERIENCE = entityCollectionChangedEventArgs.Entity.ID_EXPERIENCE;
            }
        }
        private void OnEntityAdded(object sender,
                            EntityCollectionChangedEventArgs<NIVEAU_ETUDE> entityCollectionChangedEventArgs)
        {
            NiveauxEtudes.Add(entityCollectionChangedEventArgs.Entity);
            if (candidatparam == null)
                Candidat.ID_ETUDE = NiveauxEtudes[0].ID_ETUDE;
            else
            {
                if (entityCollectionChangedEventArgs.Entity.ID_ETUDE == candidatparam.ID_ETUDE)
                    Candidat.ID_ETUDE = entityCollectionChangedEventArgs.Entity.ID_ETUDE;
            }
        }

        private void OnEntityAdded(object sender, EntityCollectionChangedEventArgs<TEST_TECHNIQUE> e)
        {
            Tests.Add(e.Entity);
        }
        private void OnEntityAdded(object sender, EntityCollectionChangedEventArgs<STATUT_CANDIDAT> e)
        {
        }
        private void OnEntityAdded(object sender, EntityCollectionChangedEventArgs<CANDIDAT_TAG> e)
        {
            if (!CandidatTagListe.Contains(e.Entity))
                CandidatTagListe.Add(e.Entity);
        }
        private void OnEntityRemoved(object sender, EntityCollectionChangedEventArgs<CANDIDAT_TAG> e)
        {
            if (CandidatTagListe.Contains(e.Entity))
                CandidatTagListe.Remove(e.Entity);
        }
        private void OnEntityAdded(object sender, EntityCollectionChangedEventArgs<TAG> e)
        {
            Tags.Add(e.Entity);
            TagsName.Add(e.Entity.NOM);
        }

        private void RemplirTestPractice()
        {
            TestsPractice.Clear();
            //foreach (TEST_TECHNIQUE item in Practice.TEST_TECHNIQUE)
            //{
            //    TestsPractice.Add(item);
            //}
            foreach (TEST_TECHNIQUE item in Tests.Where(t => t.ID_PRACTICE == Practice.ID_PRACTICE))
            {
                TestsPractice.Add(item);
            }
            if (TestsPractice.Count() > 0)
            {
                Candidat.ID_TEST = TestsPractice.First().ID_TEST;
                Test = TestsPractice.First();
            }
            else
                Candidat.ID_TEST = -1;
        }
        private void SupressionTag()
        {
        }
        #endregion





    }
}
